// pages/comment/comment.js
const app = getApp()
import Api from '../../utils/api';
import util from '../../utils/util';
Page({

  /**
   * 页面的初始数据 
   */
  data: {
    commentPage: 1, //评价起始页
    commentSize: 10, //评价每页显示大小
    commentList: [], //评价列表
    commentTotalNum: 0, //评价总条数
    goodsId: "",
    refreshFlag:false,//刷新标记
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _this = this;
    console.log(options)
    _this.setData({
      goodsId: options.goodsId
    })
    this.queryGoodsComment(options.goodsId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    if (this.data.refreshFlag) {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
      this.setData({
        refreshFlag: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
  //刷新数据
  refreshData(data){
    console.log('ssssssssssssssssssssssssssssss', data);
    var commentList = this.data.commentList;
    if ("thumb" == data.type){
      commentList.forEach((n,i)=>{
        if (data.id == n.id){
          n.alreadyThumbs = data.thumb;
          this.setData({
            commentList: commentList
          });
        }
      });
    } else if ("commentCount" == data.type){
      commentList.forEach((n, i) => {
        if (data.id == n.id) {
          n.commentCount = data.count;
          this.setData({
            commentList: commentList
          });
        }
      });
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    //console.log("上拉刷新")
    let _this = this;
    if (this.data.refreshFlag){
      console.log("请休息一会~");
      return;
    }
    _this.setData({
      commentPage: 1,
      commentList: [],
      refreshFlag:true
    })
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    if (_this.data.goodsId){
      _this.queryGoodsComment(_this.data.goodsId, function () {
        setTimeout(function () {
          // 隐藏导航栏加载框
          wx.hideNavigationBarLoading();
          // 停止下拉动作
          wx.stopPullDownRefresh();
          _this.setData({
            refreshFlag: false
          });
        }, 2000)
      })
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    // console.log("下拉加载")
    let _this = this;
    if (_this.data.commentPage < _this.data.commentTotalNum) {
      _this.setData({
        commentPage: _this.data.commentPage + 1,
      })
      wx.showLoading({
        title: '加载中',
      })
      if (_this.data.goodsId){
       _this.queryGoodsComment(_this.data.goodsId, function () {
         setTimeout(function () {
           wx.hideLoading()
         }, 1000)
       });
     }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    app.globalData.otherPageHideFlag = true;
  },
  //查询商品评论
  queryGoodsComment(goodsId, callback) {
    var _this = this;
    //组装查询条件
    let data = {
      goodsId: goodsId,
      isDel: false,
    };
    //请求数据
    Api.queryGoodsComment(this.data.commentPage, this.data.commentSize, data).then(res => {
      console.log('商品评论', res)
      //格式化时间
      var dataList = res.data.dataList;
      dataList.forEach((n,i)=>{
        n.formatDate = util.timeCalculation(new Date(n.createTime));
      });
      //数据绑定
      _this.setData({
        commentList: _this.data.commentList.concat(res.data.dataList),
        commentTotalNum: res.data.numTotal
      });
    });
    (callback && (typeof callback) === "function") && callback();
  },
  /**   
   * 预览图片  
   */
  previewImage: function(e) {
    var current = e.target.dataset.src;
    var srcs = e.target.dataset.srcs;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: srcs // 需要预览的图片http链接列表  
    })
  },
  /**   
   * 评论点赞  
   */
  commentThumbsUp: function(e) {
    console.log(e.currentTarget.dataset.index);
    let commentList = this.data.commentList;
    let item = commentList[e.currentTarget.dataset.index];
    item.thumbsUpCount = item.alreadyThumbs ? --item.thumbsUpCount : ++item.thumbsUpCount;
    item.alreadyThumbs = !item.alreadyThumbs;
    
    console.log(item)
    this.setData({
      commentList: commentList
    })
    Api.commentThumbsUp(item.id).then(res=>{
      console.log(res);
    });
  },
  //跳转评价回复页面
  gotoCommentReplyPage(e){
    let commentList = this.data.commentList;
    let item = commentList[e.currentTarget.dataset.index];
    console.log(item);
    wx.navigateTo({
      url: `/pages/comment/commentDetails?id=${item.id}`,
    })
  }
})